//
//  LoginProtocolViewController.m
//  YXFinance
//
//  Created by uplooking on 2018/10/31.
//  Copyright © 2018年 CRLong. All rights reserved.
//

#import "LoginProtocolViewController.h"

@interface LoginProtocolViewController ()
<
WKUIDelegate,
WKNavigationDelegate
>
{
    WKWebView * webView;
    UIScrollView * scrollView;
}
@end

@implementation LoginProtocolViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"报名须知";
    [self setBackLeftBarButtonItemAction:@selector(backAction:)];
    [self initView];
    [self startSKV];
}

- (void)backAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)initView{

    scrollView=[[UIScrollView alloc]init];
    [self.view addSubview:scrollView];
    [scrollView setShowsHorizontalScrollIndicator:NO];
    [scrollView setShowsVerticalScrollIndicator:NO];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    
    UIView *vContainer = [UIView new];
    [scrollView addSubview:vContainer];
    [vContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.width.equalTo(scrollView);
    }];

    NSString * url = [UserCenterHelper shareInstance].configureModel.enroll_note_url;
    
     webView = [[WKWebView alloc] init];
    [webView setUIDelegate:self];
    [webView setNavigationDelegate:self];
    webView.opaque = NO;
    webView.scrollView.scrollEnabled = NO;
    NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [webView loadRequest:request];
    [vContainer addSubview:webView];
    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(vContainer);
    }];
    
    UIView * bomView = [[UIView alloc]init];
    [bomView setBackgroundColor:[UIColor whiteColor]];
    [vContainer addSubview:bomView];
    [bomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(webView.mas_bottom);
        make.left.right.equalTo(vContainer);
        make.height.mas_equalTo(150 * kAutoScanleX);
    }];
    
    
    UIButton * agreenBtn =[[UIButton alloc]init];
    [agreenBtn setBackgroundColor:HexRGBA(0xb99c6d, 1)];
    [agreenBtn addTarget:self action:@selector(agreenBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [agreenBtn.layer setMasksToBounds:YES];
    [agreenBtn.layer setCornerRadius:8.0 * kAutoScanleX];
    [agreenBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [agreenBtn setTitle:@"同意" forState:UIControlStateNormal];
    [agreenBtn.titleLabel setFont:[UIFont systemFontOfSize:16 * kAutoScanleX]];
    [bomView addSubview:agreenBtn];
    [agreenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bomView).offset(20 * kAutoScanleX);
        make.height.mas_equalTo(45 * kAutoScanleX);
        make.centerX.equalTo(bomView);
        make.width.mas_equalTo(576/2.0 * kAutoScanleX);
    }];
    
    UIButton * noAgreenBtn =[[UIButton alloc]init];
    [noAgreenBtn setBackgroundColor:HexRGBA(0xb99c6d, 1)];
    [noAgreenBtn addTarget:self action:@selector(noAgreenBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [noAgreenBtn.layer setMasksToBounds:YES];
    [noAgreenBtn.layer setCornerRadius:8.0 * kAutoScanleX];
    [noAgreenBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [noAgreenBtn setTitle:@"不同意" forState:UIControlStateNormal];
    [noAgreenBtn.titleLabel setFont:[UIFont systemFontOfSize:16 * kAutoScanleX]];
    [bomView addSubview:noAgreenBtn];
    [noAgreenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bomView).offset(-20 * kAutoScanleX);
        make.height.mas_equalTo(45 * kAutoScanleX);
        make.centerX.equalTo(bomView);
        make.width.mas_equalTo(576/2.0 * kAutoScanleX);
    }];
    
    
    [vContainer mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bomView);
    }];
    
    
}

#pragma mark ------ < WKUIDelegate,WKNavigationDelegate > ------
#pragma mark
/**  < 法2 >  */
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    
    [self stopSKV];
    
    [webView evaluateJavaScript:@"document.body.scrollHeight" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
        
        CGFloat documentHeight = [result doubleValue];
        
        [webView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(documentHeight);
        }];
    }];
    
}


#pragma mark -- 同意
- (void)agreenBtnAction:(UIButton *)sender{
    
    UserModel * model = [UserCenterHelper shareInstance].userModel;
    
    if (model.signup.length == 0) {
        
        ApplyOldViewController * vc = [[ApplyOldViewController alloc]init];
        
        [self.navigationController pushViewController:vc animated:YES];
        
    }else{
        
        SignUpSuccessViewController * vc = [[SignUpSuccessViewController alloc]init];
        
        [self.navigationController pushViewController:vc animated:YES];
        
    }

}

#pragma mark -- 不同意
- (void)noAgreenBtnAction:(UIButton *)sender{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}


@end
